//
//  HLImageHolder.m
//  HLImageHolderDemo
//
//  Created by 常洪亮 on 16/12/22.
//  Copyright © 2016年 yixuanshuo. All rights reserved.
//

#import "HLImageHolder.h"
#import "HLSpreadPhotoController.h"
#import "UIImageView+WebCache.h"
#define MARGIN 3
@interface HLImageHolder()
{
    NSMutableArray *_imagesArr;
}
@end
@implementation HLImageHolder

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _imagesArr = [NSMutableArray arrayWithCapacity:0];
        _maxWidthForOneLine = frame.size.width/4*3;
        _maxHeightForOneLine = _maxWidthForOneLine;
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBigImage)];
        [self addGestureRecognizer:tap];
        _placeHodlerImage = [UIImage imageNamed:@""];
        
    }
    return self;
}
-(void)showBigImage{
    HLSpreadPhotoController *spreadVC = [[HLSpreadPhotoController alloc] init];
    
    if (_localImages) {
        spreadVC.localImages = _localImages;
    }else if (_webImages){
        spreadVC.webImages = _webImages;
    }
    CATransition * animation = [CATransition animation];
    
    animation.duration = 0.5;    //  时间
    
    /**  type：动画类型
     *  pageCurl       向上翻一页
     *  pageUnCurl     向下翻一页
     *  rippleEffect   水滴
     *  suckEffect     收缩
     *  cube           方块
     *  oglFlip        上下翻转
     */
//    animation.type = @"oglFlip";
    
    /**  type：页面转换类型
     *  kCATransitionFade       淡出
     *  kCATransitionMoveIn     覆盖
     *  kCATransitionReveal     底部显示
     *  kCATransitionPush       推出
     */
    animation.type = kCATransitionFade;
    
    //PS：type 更多效果请 搜索： CATransition
    
    /**  subtype：出现的方向
     *  kCATransitionFromRight       右
     *  kCATransitionFromLeft        左
     *  kCATransitionFromTop         上
     *  kCATransitionFromBottom      下
     */
    animation.subtype = kCATransitionFromRight;
    
    [self.window.layer addAnimation:animation forKey:nil];
    [self.window.rootViewController presentViewController:spreadVC animated:YES completion:^{
        
    }];
}
-(void)setMaxWidthForOneLine:(CGFloat)maxWidthForOneLine{
    if (maxWidthForOneLine > self.frame.size.width-2*MARGIN) {
        _maxWidthForOneLine = self.frame.size.width-2*MARGIN;
    }else{
        _maxWidthForOneLine = maxWidthForOneLine;
    }
}
-(void)setMaxHeightForOneLine:(CGFloat)maxHeightForOneLine{
    if (maxHeightForOneLine > self.frame.size.width-2*MARGIN) {
        _maxHeightForOneLine = self.frame.size.width-2*MARGIN;
    }else{
        _maxHeightForOneLine = maxHeightForOneLine;
    }
}

-(void)setLocalImages:(NSArray *)localImages{
    _localImages = localImages;
    [self createUI];
}
-(void)setWebImages:(NSArray *)webImages{
    _webImages = webImages;
    [self createUI];
}
-(void)createUI{
    if (_webImages) {
        //网络图片
        //1张图
        NSLog(@"加载网络图片");
        if (_webImages.count == 1) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [self addSubview:imageView];
            [_imagesArr addObject:imageView];
            imageView.backgroundColor = [UIColor lightGrayColor];
            
            [imageView sd_setImageWithURL:[NSURL URLWithString:_webImages[0]] placeholderImage:_placeHodlerImage];
            [imageView sd_setImageWithURL:[NSURL URLWithString:_webImages[0]]  placeholderImage:_placeHodlerImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//                UIImage *image = imageView.image;
                if (image.size.width > _maxWidthForOneLine) {
                    //宽度超出范围
                    imageView.frame = CGRectMake(MARGIN, MARGIN, _maxWidthForOneLine, image.size.height/image.size.width*_maxWidthForOneLine);
                }else{
                    //宽度没超出范围
                    imageView.frame = CGRectMake(MARGIN, MARGIN, image.size.width, image.size.height);
                }
                if (image.size.height > _maxHeightForOneLine) {
                    //高度超出范围
                    imageView.frame = CGRectMake(MARGIN, MARGIN, imageView.frame.size.width/imageView.frame.size.height*_maxHeightForOneLine, _maxHeightForOneLine);
                }else{
                    //高度没超出范围
                    imageView.frame = CGRectMake(MARGIN, MARGIN, imageView.frame.size.width, imageView.frame.size.height);
                }
                self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, imageView.frame.size.width+2*MARGIN, imageView.frame.size.height+2*MARGIN);
            }];
            
            
        }
        
        
        //2-9
        if (_webImages.count >= 2 && _webImages.count <=9) {
            CGFloat width = (self.frame.size.width-4*MARGIN)/3;
            CGFloat height = width;
            for (int i = 0; i < _webImages.count; i++) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(MARGIN + i%3*(width+MARGIN), MARGIN + i/3*(height+MARGIN), width, height)];
                [self addSubview:imageView];
                [_imagesArr addObject:imageView];
                imageView.backgroundColor = [UIColor lightGrayColor];
                [imageView sd_setImageWithURL:[NSURL URLWithString:_webImages[i]] placeholderImage:_placeHodlerImage];
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                //            imageView
                if (i == _webImages.count - 1) {
                    if (_webImages.count == 2) {
                        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width*2+MARGIN*3, imageView.frame.origin.y+imageView.frame.size.height+MARGIN);
                        return;
                    }
                    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, imageView.frame.origin.y+imageView.frame.size.height+MARGIN);
                    
                }
                
            }
        }

    }else if(_localImages){
        //本地图片
        NSLog(@"加载本地图片");
        //1张图
        if (_localImages.count == 1) {
            UIImageView *imageView = [[UIImageView alloc] init];
            [self addSubview:imageView];
            [_imagesArr addObject:imageView];
            imageView.backgroundColor = [UIColor lightGrayColor];
            
            imageView.image = [UIImage imageNamed:_localImages[0]];
            UIImage *image = imageView.image;
            if (image.size.width > _maxWidthForOneLine) {
                //宽度超出范围
                imageView.frame = CGRectMake(MARGIN, MARGIN, _maxWidthForOneLine, image.size.height/image.size.width*_maxWidthForOneLine);
            }else{
                //宽度没超出范围
                imageView.frame = CGRectMake(MARGIN, MARGIN, image.size.width, image.size.height);
            }
            if (image.size.height > _maxHeightForOneLine) {
                //高度超出范围
                imageView.frame = CGRectMake(MARGIN, MARGIN, imageView.frame.size.width/imageView.frame.size.height*_maxHeightForOneLine, _maxHeightForOneLine);
            }else{
                //高度没超出范围
                imageView.frame = CGRectMake(MARGIN, MARGIN, imageView.frame.size.width, imageView.frame.size.height);
            }
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, imageView.frame.size.width+2*MARGIN, imageView.frame.size.height+2*MARGIN);
            
        }
        
        
        //2-9
        if (_localImages.count >= 2 && _localImages.count <=9) {
            CGFloat width = (self.frame.size.width-4*MARGIN)/3;
            CGFloat height = width;
            for (int i = 0; i < _localImages.count; i++) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(MARGIN + i%3*(width+MARGIN), MARGIN + i/3*(height+MARGIN), width, height)];
                [self addSubview:imageView];
                [_imagesArr addObject:imageView];
                imageView.backgroundColor = [UIColor lightGrayColor];
                imageView.image = [UIImage imageNamed:_localImages[i]];
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                //            imageView
                if (i == _localImages.count - 1) {
                    if (_localImages.count == 2) {
                        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width*2+MARGIN*3, imageView.frame.origin.y+imageView.frame.size.height+MARGIN);
                        return;
                    }
                    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, imageView.frame.origin.y+imageView.frame.size.height+MARGIN);
                    
                }
                
            }
        }

    }else{
        NSLog(@"请为_webImages或者_localImages赋值!");
    }
    }
@end
